package com.jj.fly.utils;

import javax.servlet.http.HttpServletRequest;

import org.apache.commons.lang3.StringUtils;

public class BrowserClientUtils {

    /**
     * 获取浏览器类型
     * 
     * @param request
     * @return
     */
    public static String getBrowserClient(HttpServletRequest request) {
        try {
            String agent = request.getHeader("User-Agent");
            if (StringUtils.isNotEmpty(agent)) {
                return getBrowserName(agent);
            }
            return "Others";
        } catch (Exception e) {
            return "Others";
        }


    }

    /**
     * 判断浏览器类型
     * 
     * @param agent
     * @return
     */
    private static String getBrowserName(String agent) {
        if (agent.indexOf("msie 7") > 0) {
            return "ie7";
        } else if (agent.indexOf("msie 8") > 0) {
            return "ie8";
        } else if (agent.indexOf("msie 9") > 0) {
            return "ie9";
        } else if (agent.indexOf("msie 10") > 0) {
            return "ie10";
        } else if (agent.indexOf("msie") > 0) {
            return "ie";
        } else if (agent.contains("chrome")) {
            return "Chrome";
        } else if (agent.indexOf("opera") > 0) {
            return "opera";
        } else if (agent.indexOf("opera") > 0) {
            return "opera";
        } else if (agent.indexOf("firefox") > 0) {
            return "firefox";
        } else if (agent.indexOf("webkit") > 0) {
            return "webkit";
        } else if (agent.indexOf("gecko") > 0 && agent.indexOf("rv:11") > 0) {
            return "ie11";
        } else {
            return "Others";
        }
    }
}
